#ifndef OPERATORSTACK_H
#define OPERATORSTACK_H
#include "CalcStack.h"
#include <QChar>
#include <QMap>



//通过类模板派生普通子类
//https://blog.csdn.net/tianguiyuyu/article/details/104698035

//操作符栈
class OperatorStack: public CalcStack<QChar>
{
public:
    OperatorStack();
    ~OperatorStack();
    bool push(const QChar & op);//重写父类的 push 函数
    bool isValid(const QChar & op) const;//判断某个操作符是否合法
private:
    const QMap<QChar, int> * m_priority;//运算符优先级
};


//初始化优先级表
inline OperatorStack::OperatorStack(): m_priority
                                       (new QMap<QChar, int>({{u'×', 1}, {u'÷', 1}, {'+', 2}, {'-', 2}, {'=', 3}}))
{

}

inline OperatorStack::~OperatorStack()
{
    delete m_priority;
}


inline bool OperatorStack::push(const QChar & op)
{
    if (isFull() || !isValid(op))
        return false;
    //若栈为空，或当前待入栈操作符的优先级高于当前栈顶操作符的优先级，则入栈
    if (isEmpty() || m_priority->constFind(op).value() < m_priority->constFind(top()).value())
    {
        CalcStack::push(op);
        return true;
    }
    //栈不为空，且当前待入栈操作符的优先级低于当前栈顶，则不入栈
    else
        return false;
}

inline bool OperatorStack::isValid(const QChar &op) const
{
    return m_priority->constFind(op) != m_priority->constEnd();
}

#endif // OPERATORSTACK_H
